//
//  ShareSDKIntegrateViewController.m
//  SDKSample
//
//  Created by 小肥羊 on 15/7/22.
//  Copyright (c) 2015年 Zhu‘s Game House. All rights reserved.
//

#import "ShareSDKIntegrateViewController.h"




@interface ShareSDKIntegrateViewController ()

/**
 *  昵称标签
 */
@property (weak, nonatomic) IBOutlet UILabel *nicknameLabel;

/**
 *  头像视图
 */
@property (weak, nonatomic) IBOutlet UIImageView *avatarImageView;

/**
 *  关于标签
 */
@property (weak, nonatomic) IBOutlet UILabel *aboutMeLabel;

/**
 *  提示文字标签
 */
@property (weak, nonatomic) IBOutlet UILabel *tipsLabel;


/**
 *  用户列表
 */
@property (nonatomic, strong) NSMutableArray *users;

@end

@implementation ShareSDKIntegrateViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.

    [self refreshUserInfo];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (IBAction)sdkLogin:(UIButton *)sender {
    [SSEThirdPartyLoginHelper loginByPlatform:SSDKPlatformSubTypeWechatSession
                                   onUserSync:^(SSDKUser *user, SSEUserAssociateHandler associateHandler) {
                                       
                                       //在此回调中可以将社交平台用户信息与自身用户系统进行绑定，最后使用一个唯一用户标识来关联此用户信息。
                                       //在此示例中没有跟用户系统关联，则使用一个社交用户对应一个系统用户的方式。将社交用户的uid作为关联ID传入associateHandler。
                                       associateHandler (user.uid, user, user);
                                       
                                   }
                                onLoginResult:^(SSDKResponseState state, SSEBaseUser *user, NSError *error) {
                                    
                                    if (state == SSDKResponseStateSuccess)
                                    {
                                        //判断是否已经在用户列表中，避免用户使用同一账号进行重复登录
                                    }
                                    [self refreshUserInfo];
                                }];

}

- (IBAction)logout:(id)sender {
    [[XFYShareSDKAdapter sharedManager] LoginOut];
    [self refreshUserInfo];
}

- (IBAction)showInfo:(id)sender {
    ShareSDKUserInfo *currentUser = [[XFYShareSDKAdapter sharedManager] currentUser];
    
    if (currentUser)
    {
        NSLog(@"----%@----",currentUser.socialUsers);
        [self refreshUserInfo];
    }
}


/**
 *  刷新用户信息
 */
- (void)refreshUserInfo
{
    ShareSDKUserInfo *currentUser = [[XFYShareSDKAdapter sharedManager] currentUser];
    
    if (currentUser)
    {
        self.tipsLabel.hidden = YES;
        self.nicknameLabel.text = currentUser.nickname;
        if (currentUser.avatar)
        {
            NSString *observer = [self description];
            __weak ShareSDKIntegrateViewController *theController = self;
            [[MOBFImageService sharedInstance] getImageWithURL:[NSURL URLWithString:currentUser.avatar]
                                                      observer:observer
                                                     onLoading:nil
                                                      onResult:^(NSData *imageData) {
                                                          
                                                          theController.avatarImageView.image = [[UIImage alloc] initWithData:imageData];
                                                          [[MOBFImageService sharedInstance] removeObserver:observer];
                                                          
                                                      }
                                                        onFail:nil];
            self.aboutMeLabel.text = currentUser.aboutMe;
        }
    } else {
        self.tipsLabel.hidden = NO;
        self.nicknameLabel.text = @"";
        self.avatarImageView.image = nil;
        self.aboutMeLabel.text = @"";
    }
}

- (IBAction)share:(id)sender {
    //创建分享参数
    NSMutableDictionary *shareDict = [[NSMutableDictionary alloc] init];
//    [shareDict setObject:@"标题" forKey:__eShareTitle];
//    [shareDict setObject:@"内容" forKey:__eShareBody];
//    [shareDict setObject:@"http://mob.com" forKey:__eShareUrl];
//    [shareDict setObject:[UIImage imageNamed:@"shareImg.png"] forKey:__eShareImage];
    [shareDict setObject:@"你来登记，我就敢送！——千万商城购物券 向你砸来。" forKey:__eShareTitle];
    [shareDict setObject:@"你来登记，我就敢送！——千万商城购物券 向你砸来。" forKey:__eShareBody];
    [shareDict setObject:@"https://www.baidu.com" forKey:__eShareUrl];  //http://dwz.cn/ZX0us 竟然跟链接有关
    [shareDict setObject:@"http://appweb.ihk.cn/ihkapp_web/images/favourable/favourable.png"
 forKey:__eShareImage];
    
    //[[XFYShareSDKAdapter sharedManager] ShareToShareSdkUIWithKey:shareDict View:self.view];
    [[XFYShareSDKAdapter sharedManager] ShareNoUIWithKey:shareDict ShareType:SSDKPlatformSubTypeWechatTimeline onStateChanged:nil];

}


//[UIImage imageNamed:@"shareImg.png"]
//@"http://tp1.sinaimg.cn/2546880784/50/0/1"


/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
